/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.onlyxiahui.common.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

public class OnlyPropertiesUtil {

    private static Map<String, ResourceBundle> pesourceBundleMap = new ConcurrentHashMap<String, ResourceBundle>();

    /**
     * 根据classpath路径读取配置文件属性<br>
     * String address =
     * OnlyConfigUtil.getProperty("config/setting/config.properties",
     * "remote.server.address");
     *
     * @param classPath ：classpath的文件路径："/com/common/config.properties"
     * @param name
     * @return String
     */
    public static String getPropertyByClassPtah(String classPath, String name) {
        return getPropertyByClassPath(classPath, name, false);
    }

    public static String getPropertyByClassPath(String path, String name, boolean createNew) {
        ResourceBundle rb = getResourceBundleByClassPath(path, createNew);
        if (null == rb) {
            return null;
        }
        if (rb.containsKey(name)) {
            return rb.getString(name);
        } else {
            return null;
        }
    }

    public static ResourceBundle getResourceBundleByClassPath(String classPath, boolean createNew) {
        ResourceBundle rb = pesourceBundleMap.get(classPath);
        InputStream in = null;
        try {
            if (null == rb || createNew) {
                in = OnlyPropertiesUtil.class.getResourceAsStream(classPath);
                rb = new PropertyResourceBundle(in);
                pesourceBundleMap.put(classPath, rb);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return rb;

    }


    /**
     * Description 根据路径读取配置文件属性<br>
     * path根据文件路径如:<br>
     * 绝对路径"E:/resources/config.properties"<br>
     * 相对路径："resources/config.properties"<br>
     * <br>
     * Date 2019-03-30 17:31:48<br>
     *
     * @param path
     * @param name
     * @return String
     * @since 1.0.0
     */
    public static String getProperty(String path, String name) {
        return getProperty(path, name, false);
    }

    public static String getProperty(String path, String name, boolean createNew) {
        ResourceBundle rb = getResourceBundle(path, createNew);
        if (null == rb) {
            return null;
        }
        if (rb.containsKey(name)) {
            return rb.getString(name);
        } else {
            return null;
        }
    }

    public static ResourceBundle getResourceBundle(String path, boolean createNew) {
        ResourceBundle rb = pesourceBundleMap.get(path);
        InputStream in = null;
        try {
            if (null == rb || createNew) {
                if (OnlyFileUtil.isFileExists(path)) {
                    in = new BufferedInputStream(new FileInputStream(path));
                    rb = new PropertyResourceBundle(in);
                    pesourceBundleMap.put(path, rb);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return rb;
    }
}
